<?php

declare(strict_types=1);
/*
 * @Description: 个推service类
 * @Author: QianLong
 * @Date: 2019-10-13 12:39:05
 * @LastEditors    : QianLong
 * @LastEditTime   : 2024-03-07 10:26:17
 */

namespace App\Service\File;

use App\Exception\ApiException;
use OSS\Core\OssException;
use OSS\OssClient;

class AliOss
{
    protected $serverInfo = [];
    protected $ossClient;
    public function __construct($serverInfo)
    {
        $this->serverInfo = $serverInfo;
        if ($this->serverInfo['aliyun_is_internal_upload_bool'] == 1) {
            $this->serverInfo['Endpoint'] = $this->serverInfo['aliyun_region'] . '-internal.aliyuncs.com';
        } else {
            $this->serverInfo['Endpoint'] = $this->serverInfo['aliyun_region'] . '.aliyuncs.com';
        }
        $this->ossClient = new OssClient($this->serverInfo['aliyun_key_id'], $this->serverInfo['aliyun_key_secret'], $this->serverInfo['Endpoint']);
    }
    public function uploadFile($object, $Path)
    {
        //try 要执行的代码,如果代码执行过程中某一条语句发生异常,则程序直接跳转到CATCH块中,由$e收集错误信息和显示
        try {
            if (is_null($this->ossClient)) {
                exit(1);
            }
            //uploadFile的上传方法
            $oss_data = $this->ossClient->uploadFile($this->serverInfo['aliyun_bucket'], $object, $Path);
            return $oss_data;
        } catch (OssException $e) {
            throw new ApiException(-1,$e->getMessage());
        }
        //否则，完成上传操作
        return true;
    }
    public function getFileUrl($object, $timeout = 900)
    {
        if ($this->serverInfo['aliyun_alc'] == 'private') {
            return $this->getOssSecureUrl($object,$timeout);
        }else{
            return $this->getOssUrl($object);
        }
    }
    public function getOssSecureUrl($object, $timeout = 900)
    {
        if (is_null($this->ossClient)) {
            exit(1);
        }
        try {
            $signedUrl = $this->ossClient->signUrl($this->serverInfo['aliyun_bucket'], $object, $timeout);
        } catch (OssException $e) {
            throw new ApiException(-1, $e->getMessage());
        }
        if (strpos($signedUrl, 'http://') === 0) {
            $table_change = array('http://' => 'https://');
            $signedUrl = strtr($signedUrl, $table_change);
        }
        $signedUrl = str_replace('-internal', '', $signedUrl);
        if ($this->serverInfo['aliyun_is_define_url_bool'] == 1) {
            $parseUrl = parse_url($signedUrl);
            $signedUrl = str_replace($parseUrl['scheme'] . '://' . $parseUrl['host'], rtrim($this->serverInfo['aliyun_web_url'], '/'), $signedUrl);
        }
        return $signedUrl;
    }
    public function getOssUrl($object)
    {
        if ($this->serverInfo['aliyun_is_define_url_bool'] == 1) {
            $url = $this->serverInfo['aliyun_web_url'];
        } else {
            $url = 'https://' . $this->serverInfo['aliyun_bucket'] . '.' . $this->serverInfo['aliyun_region'] . '.aliyuncs.com';
        }
        $url = rtrim($url, '/');
        return $url . '/' . $object;
    }
    public function deleteOssFile($object,$bucket='')
    {
        if (is_null($this->ossClient)) {
            exit(1);
        }
        try {
            if (empty($bucket)) {
                $bucket = $this->serverInfo['aliyun_bucket'];
            }
            $signedUrl = $this->ossClient->deleteObject($bucket, $object);
            return $signedUrl;
        } catch (OssException $e) {
            throw new ApiException(-1, $e->getMessage());
        }
    }
}
